/**
 * Created by wengms on 2016/11/22.
 */

define(["angular", "underscore","framework/permit/GillionPermitModule"], function (angular, _) {
    return angular
        .module("RulePermissionModule", ["GillionPermitModule"])
        //针对界面按钮权限的控制 <div pg-per="{{XXX}}|0"> 0 1 2 2 1 0
        .directive('pgPer', function ($rootScope,Permissions,$timeout) {
            return {
                scope: false,
                restrict: 'A',
                link: function ($scope, element) {
                    $timeout(function(){
                        //主要判断按钮是否有权限,分为3种
                        //1. 可以操作,并显示  //2. 不可以操作,不显示,主要通过ID来进行判定
                        var packagePer = element.attr("pg-per"),
                            perInfos = packagePer.split("|"),
                            packageId = perInfos[0],
                            showMode = parseInt(perInfos[1]),
                            roles = [];
                        for (var key in $rootScope.$sessionAttrs.projectPermission) {
                            var packagePermissions = $rootScope.$sessionAttrs.projectPermission[key].packageRoles;
                            if (packagePermissions && packagePermissions[packageId]) {
                                roles = packagePermissions[packageId]["roles"];
                                break;
                            }
                        }
                        if (_.filter(roles, function (role) {
                                return role <= showMode;
                            }).length == 0) {
                            element.css("display", "none");
                        }
                    });

                }
            }
        })
        //div pj-per="{{XXX}}|"
        .directive('pjPer', function ($rootScope,$timeout) {
            return {
                scope: false,
                restrict: 'A',
                link: function ($scope, element) {
                    $timeout(function(){
                        var projectPer = element.attr("pj-per"),
                            perInfos = projectPer.split("|"),
                            projectId = perInfos[0],
                            showMode = parseInt(perInfos[1]),
                            projectRoles = [];
                        if ($rootScope.$sessionAttrs.projectPermission[projectId]) {
                            projectRoles = $rootScope.$sessionAttrs.projectPermission[projectId].roles;
                        }
                      if (projectRoles.length != 0 &&
                          __.filter(projectRoles, function(role) {
                            return role <= showMode;
                          }).length == 0) {

                        element.css('display', 'none');
                      }else {
                        element.css('display', 'block');
                      }
                    });

                }
            }
        });

})
